﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Vision.Common.Forms
{
    public partial class FormRotate : Form
    {
        public event EventHandler<RotateArgs> OnUpdateAngle;
        float lastAngle;
        float curAngle;
        public FormRotate(float angle)
        {
            InitializeComponent();
            textBox1.LostFocus += TextBox1_LostFocus;
            lastAngle = curAngle = angle;
            textBox1.Text = curAngle.ToString();
            trackBar1.Value = (int)(curAngle * 100);
        }

        private void TextBox1_LostFocus(object sender, EventArgs e)
        {
            float value;
            if (float.TryParse(textBox1.Text, out value))
            {
                trackBar1.Value = (int)(value * 100);
                curAngle = value;
                OnUpdateAngle?.Invoke(this, new RotateArgs(curAngle));
            }
            else
                textBox1.Text = curAngle.ToString();
            
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            OnUpdateAngle?.Invoke(this, new RotateArgs(lastAngle));
            this.Close();
        }

        private void trackBar1_Scroll(object sender, EventArgs e)
        {
            curAngle = trackBar1.Value / 100.0f;
            textBox1.Text = curAngle.ToString();
            OnUpdateAngle?.Invoke(this, new RotateArgs(curAngle));
        }
    }
    public class RotateArgs:EventArgs
    {
        public float Angle;
        public RotateArgs(float angle)
        {
            this.Angle = angle;
        }
    }
}
